package com.rachel.liveplayer.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.GridView;
import android.widget.Toast;

import com.rachel.liveplayer.R;
import com.rachel.liveplayer.bean.HomeBean;
import com.rachel.utilslib.lgview.LGAdapter;
import com.rachel.utilslib.lgview.LGViewHolder;
import com.zhengsr.viewpagerlib.GlideManager;
import com.zhengsr.viewpagerlib.anim.MzTransformer;
import com.zhengsr.viewpagerlib.bean.PageBean;
import com.zhengsr.viewpagerlib.callback.PageHelperListener;
import com.zhengsr.viewpagerlib.indicator.TransIndicator;
import com.zhengsr.viewpagerlib.view.ArcImageView;
import com.zhengsr.viewpagerlib.view.BannerViewPager;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Administrator on 2017/10/20.
 */

public class HomeFragment extends Fragment implements NavigationView.OnNavigationItemSelectedListener {
    public static final String TAG = "zsr";
    private AppCompatActivity mActivity;
    private Context mContext;
    private DrawerLayout mDrawerLayout;
    private static final int[] RES = {R.mipmap.a, R.mipmap.b, R.mipmap.c, R.mipmap.d, R.mipmap.e};

    private static final int[] GRID_ICON = {R.mipmap.tvs,R.mipmap.movie,R.mipmap.carton,
                                R.mipmap.jilu,R.mipmap.music,R.mipmap.variety,
                                R.mipmap.live,R.mipmap.collection,R.mipmap.history};
    private static final String[] GRID_TEXT = {"电视剧","电影","动漫",
                                               "纪录片","音乐","综艺",
                                                "直播","收藏","历史"};
    private GridView mGridView;
    private BannerViewPager mBannerViewPager;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_home, container, false);
        mActivity = (AppCompatActivity) getActivity();
        mContext = mActivity;
        setHasOptionsMenu(true);  //在fragment 中需要这样去处理
        initView(view);
        intiData();
        return view;
    }



    private void initView(View view) {
        NavigationView navigationView = view.findViewById(R.id.navigation);
        navigationView.setNavigationItemSelectedListener(this);
        mDrawerLayout = view.findViewById(R.id.home_drawlayout);
        Toolbar toolbar = view.findViewById(R.id.toolbar);
        mActivity.setSupportActionBar(toolbar);

        ActionBar actionBar = mActivity.getSupportActionBar();
        if (actionBar != null){
            actionBar.setDisplayHomeAsUpEnabled(true);
            actionBar.setHomeButtonEnabled(true);
        }
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(mActivity,mDrawerLayout,
                toolbar,0,0);
        toggle.syncState();


        mBannerViewPager = view.findViewById(R.id.banner_viewpager);
        TransIndicator transIndicator = view.findViewById(R.id.transindicator);

        List<Integer> images = new ArrayList<>();
        for (int i = 0; i < RES.length; i++) {
            images.add(RES[i]);
        }

        PageBean bean = new PageBean.Builder<Integer>()
                .setDataObjects(images)
                .setIndicator(transIndicator)
                .builder();
        mBannerViewPager.setPageTransformer(false,new MzTransformer());
        mBannerViewPager.setPageListener(bean, R.layout.loop_layout, new PageHelperListener() {
            @Override
            public void getItemView(View view, Object o) {
                ArcImageView arcImageView = view.findViewById(R.id.loop_arcimage);
                new GlideManager.Builder()
                        .setContext(mContext)
                        .setImgSource(o)
                        .setImageView(arcImageView)
                        .builder();
            }
        });

        mGridView = view.findViewById(R.id.home_grid);



    }

    @Override
    public void onPause() {
        super.onPause();
        if (mBannerViewPager != null){
            mBannerViewPager.onPause();
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (mBannerViewPager != null){
            mBannerViewPager.onPause();
        }
    }

    private void intiData() {
        List<HomeBean> lists = new ArrayList<>();
        for (int i = 0; i < GRID_ICON.length; i++) {
            HomeBean bean = new HomeBean();
            bean.resid = GRID_ICON[i];
            bean.text = GRID_TEXT[i];
            lists.add(bean);
        }

        LGAdapter<HomeBean> adapter = new LGAdapter<HomeBean>(mActivity,lists,
                R.layout.grid_home_item) {
            @Override
            public void convert(LGViewHolder viewHolder, HomeBean data) {
                viewHolder.setDrawable(R.id.grid_home_icon,data.resid)
                        .setText(R.id.grid_home_text,data.text);
            }
        };
        mGridView.setAdapter(adapter);
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.toolbar_menu, menu);
        super.onCreateOptionsMenu(menu, inflater);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        Log.d(TAG, "zsr --> onOptionsItemSelected: "+item.getItemId());
        switch (item.getItemId()){
            case android.R.id.home:
                mDrawerLayout.openDrawer(GravityCompat.START);
                break;
            default:
                break;
        }
        return true;
    }


    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {

        switch (item.getItemId()) {
            case R.id.nav_home:
                Toast.makeText(mContext, "Home", Toast.LENGTH_SHORT).show();

                break;
            case R.id.nav_share:
                Toast.makeText(mContext, "share", Toast.LENGTH_SHORT).show();
                break;
            case R.id.nav_aboutme:
                Toast.makeText(mContext, "AboutMe", Toast.LENGTH_SHORT).show();
                break;
            default:
                break;
        }
        mDrawerLayout.closeDrawer(Gravity.LEFT);
        return false;
    }



    public static HomeFragment newInStance(String str) {
        HomeFragment fragment = new HomeFragment();
        Bundle bundle = new Bundle();
        bundle.putString(TAG, str);
        fragment.setArguments(bundle);
        return fragment;
    }
}
